<template>
  <div class="app-container">
    <div class="header-info">
      <span class="header-text">{{ warehouseName }}</span>
      <el-divider direction="vertical" />
      <span class="header-text"> 货区编号{{ warehouseAreaNumber }} </span>
      <el-divider direction="vertical" />
      <span class="header-text"> 货架编号{{ ShelfNumber }} </span>
      <el-divider direction="vertical" />
      货架行数{{ shelfData.col }} 货架列数{{ shelfData.line }}
      <!-- 提示区 -->
      <div class="categoryTips">
        <div v-for="item in categoryList" :key="item.color" class="categoryTipsItme">
          <div class="categoryTipsItmeColor" :style="{ backgroundColor: item.color }" />
          {{ item.name }}
        </div>
      </div>
      <el-button style="margin-left:auto;" type="primary" @click="batchGeneration">全部二维码</el-button>
    </div>
    <el-row v-for="(shelfItem, row) in shelfData.resultList" :key="row" :gutter="24">
      <el-divider />
      <el-col v-for="(item, col) in shelfItem" :key="item.id" :span="2">
        <div class="location-box" :style="{ backgroundColor: boxColor[item.status] }">
          <div class="location-header" :style="{ backgroundColor: headerColor[item.status] }">
            <div>{{ row + 1 }}-{{ col + 1 }}</div>
            <div>
              <span style="cursor: pointer;" @click="openQr(item.id, row + 1, col + 1)">
                <img
                  src="@/assets/QRCodeWhite.svg"
                  style="width: 25px;height: 25px;"
                  align="absmiddle"
                ></span>
              <el-dropdown @command="command => changeState(item, command)">
                <span class="el-dropdown-link">
                  <i class="el-icon-setting" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="2">空仓</el-dropdown-item>
                  <el-dropdown-item command="1">满仓</el-dropdown-item>
                  <el-dropdown-item command="3">禁用</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
          <div v-if="item.status == 0 || item.status == 3 || item.status == 2" class="info">
            <i :class="[item.status == 3 ? 'el-icon-folder-delete' : 'el-icon-document-remove']" />
            <span>{{ item.status == 3 ? '禁用' : '空仓' }}</span>
          </div>
          <div v-else class="full-box">
            <div class="info-box" @click="showInfo(item)">
              详情></div>
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- 二维码弹窗 -->
    <qrcode ref="qrcode" title="库位二维码" />
    <!-- 全部库位二维码弹窗 -->
    <batch-qrcode ref="batchQRCode" title="库位二维码" width="78%" />
    <shelf-info-modal ref="infoModal" :title="modalTitle" :goods-info="goodsInfo" />
  </div>
</template>

<script>
import { dibootApi } from '@/utils/request'
import qrcode from './components/QRCode.vue'
import batchQrcode from '@/components/BatchQRCode.vue'
import ShelfInfoModal from './components/ShelfInfoModal.vue'
export default {
  name: 'ShelfList',
  components: {
    qrcode,
    batchQrcode,
    ShelfInfoModal
  },
  data() {
    return {
      id: '',
      warehouseName: '',
      warehouseAreaNumber: '',
      ShelfNumber: '',
      shelfData: {},
      categoryList: [
        { name: '空闲', color: '#22AC38' },
        { name: '满仓', color: '#EC3838' },
        { name: '空仓', color: '#2465FF' },
        { name: '禁用', color: '#D2D2D2' }
      ],
      boxColor: ['#e0f3e3', '#fce0e2', '#e0e9fe', '#eeeeee'],
      headerColor: ['#89d091', '#f28890', '#86a8fd', '#bdbdbd'],
      goodsInfo: [{ goodsDetailVO: { goodsCode: '123', goodsName: '123', specifications: '123' }}],
      modalTitle: '详情'
    }
  },
  created() {
    this.setData()
  },
  activated() {
    this.setData()
  },
  methods: {
    async showInfo(item) {
      console.log(item)
      const res = await dibootApi.get(`/warehouse/latticeGoods/getLatticeGoodsById/${item.id}`)
      this.goodsInfo = res.data.data
      this.modalTitle = item.col + '' + item.line
      this.$refs.infoModal.open()
    },
    setData() {
      const { id, warehouseName, warehouseAreaNumber, ShelfNumber } = this.$route.query
      this.id = id
      this.warehouseName = warehouseName
      this.warehouseAreaNumber = warehouseAreaNumber
      this.ShelfNumber = ShelfNumber
      this.getList()
    },
    // 全部库位二维码弹窗
    batchGeneration() {
      const QrData = this.shelfData.resultList.flat(2).map(item => {
        return {
          QrString: JSON.stringify({
            id: item.id,
            label: item.label,
            QRCode: 'WarehouseLocation'
          }),
          QrName: item.label
        }
      })
      this.$refs.batchQRCode.open(QrData, '库位二维码')
    },
    // 显示二维码
    openQr(id, ...arg) {
      const shelfCode = [this.warehouseAreaNumber, this.ShelfNumber, ...arg].join('-')
      const qrString = JSON.stringify({
        label: this.warehouseName + '-' + shelfCode,
        id,
        QRCode: 'WarehouseLocation'
      })
      this.$refs.qrcode.open(this.warehouseName, qrString, shelfCode)
    },
    // 状态改变
    changeState(item, status) {
      // 页面状态改变
      item.status = status
      // 后端状态改变
      dibootApi
        .put(`/warehouse/lattice/${item.id}`, { status })
        .then(result => {})
        .catch(err => {
          console.log(err)
        })
    },
    // 获取数据
    getList() {
      dibootApi.get(`/warehouse/lattice/${this.id}`).then(result => {
        this.shelfData = result.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.full-box {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 75%;
  .info-box {
    height: 100%;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    color: #70747f;
    cursor: pointer;
  }
}

.header-text {
  font-weight: bold;
}
.header-info {
  display: flex;
  padding-top: 16px;
  align-items: center;
}
.categoryTips {
  display: flex;
  .categoryTipsItme {
    display: flex;
    align-items: center;
    .categoryTipsItmeColor {
      width: 10px;
      height: 10px;
      border-radius: 2px;
      margin: 0 5px;
    }
  }
}
.el-dropdown-link {
  cursor: pointer;
  color: white;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.location-box {
  width: 100%;
  height: 110px;
  border-radius: 4px;
  // border: 1px solid #a0baf7;
  background-color: #e0e9fe;
  margin: 8px;
  .location-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #86a8fd;
    color: white;
    box-sizing: border-box;
    padding: 3px 5px;
  }
  .info {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #70747f;
    i {
      font-size: 35px;
    }
    span {
      font-size: 12px;
    }
  }
}
</style>
